<include file="Public:header" />
<include file="Public:mainNavBar" />
<include file="Public:sidebar" />

<div id="wrapper" class="wat-cf">
     <div id="main">
		<div class="block" id="block-tables">
			<div class="content">
	            <h2 class="title">{:L('Agency')}</h2>
	            <div class="inner">
	            <div style="display:blcok;">&nbsp;
					<div style="width:600px;height:40px;float: right;">
						<input id="cboxSearchName" type="checkbox" value='' > &nbsp;名字
						<input id="cboxSearchTime" type="checkbox" value='' > &nbsp;时间
						<div id="name_con_div" style="display:inline-block;">
							<input type="text" id="searchName" style="width:315px;height:28px;">	
							<button class="button" onclick="searchByName();" style="float: right;">search</button>
						</div>
						<div id="time_con_div" style="display:none;">
							start:<input id="startTime" readonly="readonly" type="text" style="width:150px;height:28px;"/>
							end:<input id="endTime" readonly="readonly" type="text" style="width:150px;height:28px;" />
							<button class="button" onclick="searchByTime();" style="float: right;">search</button>
						</div>
						
						
						
					</div>
	             </div>   
	             <p></p>
	               <table id="dataTable" class="table">
	                  <tr>
	                    <th class="first"><input id="cbox1" type="checkbox" class="checkbox toggle" value='' /></th>
	                    <th>index</th>
	                    <th>name</th>
	                    <th>email</th>
	                    <th>phone</th>
	                    <th>city</th>
	                    <th>road_name</th>
	                    <th>firstname</th>
	                    <th>lastname</th>
	                    <th>Confirmed</th>
	                    <th>Status</th>
	                    <th>createdtime</th>
	                    <th></th>
	                    <th class="last">&nbsp;</th>
	                  </tr>
	                  <tbody id='listBody'>
	                  
	                  </tbody>
	                </table>
	                <div class="actions-bar wat-cf">
	                  <div class="actions">
	                    <button class="button" type="submit" onclick="delSelectedItems();">
	                      <img src="../Public/images/icons/cross.png" alt="Delete" />Delete
	                    </button>
	                  </div>
	                  <div id="pagination" class="pagination">
	                    <!-- <span class="disabled prev_page">« Previous</span><span class="current">1</span><a rel="next" href="#">2</a><a href="#">3</a><a href="#">4</a><a href="#">5</a><a href="#">6</a><a href="#">7</a><a href="#">8</a><a href="#">9</a><a href="#">10</a><a href="#">11</a><a rel="next" class="next_page" href="#">Next »</a> -->
	                  </div>
	                </div>
	            </div>
	          </div>
		</div>
		
		<include file="Public:agencyForm" />
		<include file="Public:message" />
	</div>
</div>
<script>
var currPageIndex = 1;
var totalPages = 0;
var searchNameCondition = '';
var startTime = '';
var endTime = '';
$(function(){
	loadPage(1);
	
	//search bar
	$('#cboxSearchName').attr('checked', 'true');
	$('#cboxSearchTime').removeAttr('checked');
	$('#cboxSearchName').click(function(){
		$('#cboxSearchTime').removeAttr('checked');
		$('#time_con_div').css('display', 'none');
		$('#name_con_div').css('display', 'inline-block');
	});
	$('#cboxSearchTime').click(function(){
		$('#cboxSearchName').removeAttr('checked');
		$('#time_con_div').css('display', 'inline-block');
		$('#name_con_div').css('display', 'none');
	});
	
	//search time
	$('#startTime').datepicker({
		'dateFormat' :'yy-mm-dd',
		changeYear: true,
		changeMonth: true,
		yearRange: "2013:2050"
	});
	$('#endTime').datepicker({
		'dateFormat' :'yy-mm-dd',
		changeYear: true,
		changeMonth: true,
		yearRange: "2013:2050"
	});
	
	
	$('#cbox1').removeAttr('checked');
	$('#cbox1').click(function(){
		if ($('#cbox1').attr('checked')) {
			$("input[type='checkbox']").attr('checked', 'true');			
		} else {
			$("input[type='checkbox']").removeAttr('checked');	
		}
	});
});

function loadPage(pageIndex) {
	currPageIndex = pageIndex;
	//load data
	$.post('/Admin/Agency/loadAgencyPage',{p:pageIndex,sName:searchNameCondition,sTime:startTime,eTime:endTime},function(data){
		var obj = $.parseJSON(data);
		if (obj.status == 1) {
			loadData(obj);
		} else if(obj.status == 2) {
			$('#listBody').html('');
			showMessage(obj.msg, 'warning');
		} else if (obj.status == 0) {
			showMessage(obj.msg, 'error');
		}
	});
}

function loadData(obj) {
	var data = obj.data;
	var html = '';
	for(i=0; i<data.length; i++) {
		html = html + '<tr><td><input type="checkbox" class="checkbox" name="index" value="'+data[i].id+'" /></td>';
		html = html + '<td>'+(i+1)+'</td>';
		html = html + '<td>'+data[i].name+'</td>';
		html = html + '<td>'+data[i].email+'</td>';
		html = html + '<td>'+data[i].phone+'</td>';
		html = html + '<td>'+data[i].city+'</td>';
		html = html + '<td>'+data[i].road_name+'</td>';
		html = html + '<td>'+data[i].firstname+'</td>';
		html = html + '<td>'+data[i].lastname+ '</td>';
		var confirm = (data[i].confirmid) == 1?'confirmed':'unconfirm';
		var status = (data[i].statusid) == 1?'normal':'delete';
		html = html + '<td>'+confirm+'</td>';
		html = html + '<td>'+status+'</td>';
		html = html + '<td>'+data[i].createdtime+'</td>';
		html = html + '<td><a href="javascript:editItem('+data[i].id+');" style="cursor:poniter;"><img src="../Public/images/icons/edit.png"/></a>&nbsp;&nbsp';
		html = html + '<a href="javascript:deleteItem('+data[i].id+')" style="cursor:poniter;" ><img src="../Public/images/icons/trash.png"/></a></td></tr>';
	}
	$('#listBody').html(html);
	
	//biuld pagination
	var pagination = '';
	var page = obj.page;
	var nowPage = parseInt(page.nowPage);
	var prevPage = nowPage - 1;
	totalPages = page.totalPages;
	if (prevPage > 0) {
		pagination = '<span class="disabled prev_page" style="cursor:pointer;" onclick="javascript:loadPage('+prevPage+');">« Prev</span>';	
	} else {
		pagination = '<span class="disabled prev_page">« Prev</span>';
	}
	
	for (i=1; i<= page.totalPages; i++) {
		if (i == nowPage) {
			pagination = pagination + '<span class="current">'+i+'</span>';
		} else {
			pagination = pagination + '<a href="javascript:loadPage('+i+');">'+i+'</a>';					
		}
	}
	var nextPage = nowPage + 1;
	if (nextPage > page.totalPages) {
		pagination = pagination + '<span class="next_page">Next »</span>';	
	} else {
		pagination = pagination + '<span class="next_page" style="cursor:pointer;" onclick="javascript:loadPage('+nextPage+');">Next »</a></span>';
	}
	
	$('#pagination').html(pagination);
}

function deleteItem(id) {
	var ids = [];
	if (id != '') {
		if(confirm('是否要删除这条数据？')) {
			ids.push(id);
			doDelete(ids);		
		} else {
			return false;
		}
	}
}

function delSelectedItems() {
	var selectedItemIds = [];
	var tempId = '';
	$('input[type="checkbox"]').each(function(){
		tempId = $(this).val();
		if (tempId != '' && $(this).attr('checked')) {
			//selectedItemIds = selectedItemIds + tempId + ',';
			selectedItemIds.push(tempId);
		}
	});
	//selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
	if (selectedItemIds.length > 0) {
		if (confirm('是否要删除被选中的数据？')) {
			doDelete(selectedItemIds);
		} else {
			return false;
		}
	}
}
function doDelete(ids) {
	$.post('/Admin/Agency/doDelete', {ids:ids}, function(data){
		var obj = $.parseJSON(data);
		if (obj.status == 1) {
			loadPage(currPageIndex);
		} else {
			alert(obj.msg);
		}
	});
}

function editItem(id) {
	$.post('/Admin/Agency/getAgencyInfo', {id:id}, function(data){
		var obj = $.parseJSON(data);
		if (obj.status == 1) {
			openEditDialog(obj.data);	
		}
	});
}

function searchByName() {
	searchNameCondition = $('#searchName').val();
	loadPage(1);
}
function searchByTime() {
	searchNameCondition = '';
	startTime = $('#startTime').val();
	endTime = $('#endTime').val();
	loadPage(1);
}
</script>
<include file="Public:footer" />
          